﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfAnalyzerLib;

namespace PerfAnalyzerUI
{
	class DataGridViewHelpPerflog : DataGridViewHelper
	{
		public const string ColLogFilename = "col_PlManag_LogFile";
		public const string ColLogSize = "col_PlManag_LogSize";
		public const string ColStatus = "col_PlManag_Status";


		//private const string _colLogFilename = "Log File";
		//private const string _colLogSize = "File Size";
		//private const string _colStatus = "Status";

		delegate void UpdateStatusColumnCallBack(DataGridViewRow row, string status);

		public DataGridViewHelpPerflog(DataGridView trackedDataGridView, Form partentForm)
			: base(trackedDataGridView, partentForm)
		{
		}

		public override DataGridViewRow AddRow(object tag)
		{
			PerfLog perflog = (PerfLog)tag;

			_dataGridView.Rows.Add();
			int rowIndex = _dataGridView.RowCount - 1;
			DataGridViewRow row = _dataGridView.Rows[rowIndex];
			row.Cells[ColLogFilename].Value = perflog.Filename;
			row.Cells[ColLogSize].Value = string.Format(new FileSizeFormatProvider(), "{0:fs}", perflog.FileInfo.Length);
			row.Cells[ColStatus].Value = "Not Parsed";
			row.Tag = perflog;
			_dataGridView.Refresh();

			TagToRow.Add(perflog, row);

			return row;
		}

		public override void AddRows(IEnumerable<object> tags)
		{
			throw new NotImplementedException();
		}

		//public void Refresh()
		//{
		//	_dataGridView.Rows.Clear();
		//	TagToRow.Clear();

		//	foreach (PerfLog perflog in PerfLogManager.PerfLogs)
		//	{
		//		AddRow(perflog);
		//	}

		//	_dataGridView.Refresh();
		//}

		public override void RemoveRow(DataGridViewRow row)
		{
			PerfLogManager.RemovePerlog((PerfLog)row.Tag);
			_dataGridView.Rows.Remove(row);
			TagToRow.Remove(row.Tag);
		}

		public override void Clear()
		{
			_dataGridView.Rows.Clear();
			PerfLogManager.ClearPerflogList();
			TagToRow.Clear();
		}

		public void UpdateStatusColumn(DataGridViewRow row, string status)
		{
			if (_dataGridView.InvokeRequired)
			{
				var d = new UpdateStatusColumnCallBack(UpdateStatusColumn);
				_parentForm.Invoke(d, new object[] { row, status });
			}
			else
			{
				row.Cells[ColStatus].Value = status;
				_dataGridView.Refresh();
			}			
		}


	}
}
